﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Transactions;
using _4T.Data;

namespace _4T.DAL
{
    public class MedicalInfoDAL
    {
        public MedicalInfoDAL() { }

        public int InsertMedicalInfo(string diagnosis, DateTime medicalTime, string treatmentMethod, string situation,
            string support, string note)
        {
            int result = 0;
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    SqlConnection conn = new SqlConnection(conString);
                    SqlCommand cmd = new SqlCommand();
                    try
                    {
                        conn.Open();
                        cmd.CommandText = "MedicalInfoInsert";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@Diagnosis", SqlDbType.NText).Value = diagnosis;
                        cmd.Parameters.Add("@MedicalTime", SqlDbType.DateTime).Value = medicalTime;
                        cmd.Parameters.Add("@TreatmentMethod", SqlDbType.NText).Value = treatmentMethod;
                        cmd.Parameters.Add("@Situation", SqlDbType.NText).Value = situation;
                        cmd.Parameters.Add("@Support", SqlDbType.NText).Value = support;
                        cmd.Parameters.Add("@Note", SqlDbType.NText).Value = note;
                        cmd.Connection = conn;

                        result = Convert.ToInt32(cmd.ExecuteScalar());
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        cmd.Dispose();
                        if (conn.State != ConnectionState.Closed)
                            conn.Close();
                    }
                    scope.Complete();
                }
            }

            return result;
        }

        public int UpdateMedicalInfo(string diagnosis, DateTime medicalTime, string treatmentMethod, string situation,
            string support, string note, string profileInfoCode)
        {
            int result = 0;
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    SqlConnection conn = new SqlConnection(conString);
                    SqlCommand cmd = new SqlCommand();
                    try
                    {
                        conn.Open();
                        cmd.CommandText = "MedicalInfoUpdate2";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@Diagnosis", SqlDbType.NText).Value = diagnosis;
                        cmd.Parameters.Add("@MedicalTime", SqlDbType.DateTime).Value = medicalTime;
                        cmd.Parameters.Add("@TreatmentMethod", SqlDbType.NText).Value = treatmentMethod;
                        cmd.Parameters.Add("@Situation", SqlDbType.NText).Value = situation;
                        cmd.Parameters.Add("@Support", SqlDbType.NText).Value = support;
                        cmd.Parameters.Add("@Note", SqlDbType.NText).Value = note;
                        cmd.Parameters.Add("@ProfileInfoCode", SqlDbType.VarChar, 50).Value = profileInfoCode;
                        cmd.Connection = conn;

                        result = Convert.ToInt32(cmd.ExecuteScalar());
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        cmd.Dispose();
                        if (conn.State != ConnectionState.Closed)
                            conn.Close();
                    }
                    scope.Complete();
                }
            }

            return result;
        }

        public MedicalInfo GetMedicalInfo(string profileCode)
        {
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();

                MedicalInfo medical = null;
                SqlDataReader reader = null;
                try
                {
                    conn.Open();
                    cmd.CommandText = "MedicalInfoGet";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ProfileInfoCode", SqlDbType.VarChar, 50).Value = profileCode;
                    cmd.Connection = conn;

                    reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        reader.Read();
                        medical = GetMedicalFromReader(reader);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }

                return medical;
            }

            return null;
        }

        public MedicalInfo GetMedicalInfo2(string profileCode)
        {
            ConnectionStringSettings ConnectionStringSettings =
              ConfigurationManager.ConnectionStrings["cnnString4T"];
            if (ConnectionStringSettings != null && !string.IsNullOrEmpty(ConnectionStringSettings.ConnectionString.Trim()))
            {
                string conString = ConnectionStringSettings.ConnectionString;

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand();

                MedicalInfo medical = null;
                SqlDataReader reader = null;
                try
                {
                    conn.Open();
                    cmd.CommandText = "MedicalInfoGet2";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ProfileCode", SqlDbType.VarChar, 50).Value = profileCode;
                    cmd.Connection = conn;

                    reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        reader.Read();
                        medical = GetMedicalFromReader(reader);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    cmd.Dispose();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }

                return medical;
            }

            return null;
        }

        private MedicalInfo GetMedicalFromReader(SqlDataReader reader)
        {
            string diagnosis = reader.GetString(0);

            DateTime time = new DateTime();
            if (reader.GetValue(1) != DBNull.Value)
                time = reader.GetDateTime(1);

            string treatment = string.Empty;
            if (reader.GetValue(2) != DBNull.Value)
                treatment = reader.GetString(2);

            string situation = string.Empty;
            if (reader.GetValue(3) != DBNull.Value)
                situation = reader.GetString(3);

            string support = string.Empty;
            if (reader.GetValue(4) != DBNull.Value)
                support = reader.GetString(4);

            string note = string.Empty;
            if (reader.GetValue(5) != DBNull.Value)
                note = reader.GetString(5);

            return new MedicalInfo(diagnosis, time, treatment, situation, support, note);
        }
    }
}